package com.ruoyi.system.service.impl;

import com.ruoyi.generator.handler.NotFoundException;
import com.ruoyi.system.domain.Article;
import com.ruoyi.system.domain.vo.DetailedArticle;
import com.ruoyi.system.domain.vo.FirstPageArticle;
import com.ruoyi.system.domain.vo.ShowArticle;
import com.ruoyi.system.mapper.ArticleMapper;
import com.ruoyi.system.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleDao;

    @Override
    public List<Article> getAllArticles() {
        return articleDao.getAllArticleQuery();
    }

    @Override
    public FirstPageArticle getArticleById(Long id) {
        return articleDao.getArticleById(id);
    }

    @Override
    public List<ShowArticle> getArticleTagById(Long typeId) {
        return articleDao.getArticleByTagId(typeId);
    }
    @Override
    public List<FirstPageArticle> getArticleTagByUserId(Long userid){
        return articleDao.getArticleTagByUserId(userid);
    };
    @Override
    public int saveArticle(Article article) {
        article.setCreateTime(new Date());
        article.setUpdateTime(new Date());
        article.setViews(0);
        article.setCommentCount(0);
        return articleDao.saveArticle(article);
    }

    @Override
    public List<FirstPageArticle> getArticleInfoId(String query,Long tagId) {
        return articleDao.getArticleInfoId(query,tagId);
    }

    @Override
    public List<FirstPageArticle> getArticleTagId(Long tagId) {
        return articleDao.getArticleTagId(tagId);
    }

    @Override
    public int updateArticle(ShowArticle showArticle) {
        showArticle.setUpdateTime(new Date());
        return articleDao.updateArticle(showArticle);
    }

    @Override
    public void deleteArticle(Long id) {
        articleDao.deleteArticle(id);
    }

    @Override
    public List<FirstPageArticle> getFirstAllArticles() {
        return articleDao.getFirstAllArticles();
    }

    @Override
    public List<FirstPageArticle> getSearchArticles(String query) {
        return articleDao.getSearchArticles(query);
    }

    @Override
    public DetailedArticle getDetailedArticle(Long id){
        DetailedArticle detailedArticle = articleDao.getDetailedArticle(id);
        if (detailedArticle == null) {
            throw new NotFoundException("该话题不存在");
        }
        articleDao.updateViews(id);
        articleDao.getCommentCountById(id);
        return detailedArticle;
    }

    @Override
    public Integer getArticleViewTotal() {
        return articleDao.getArticleViewTotal();
    }
}
